<script setup>
import { onMounted, ref } from 'vue';
import { logApi } from '@/api/log'

// 日志表格
const logList = ref([])
// 当前页码
const currentPage = ref(1)
// 每页显示多少条
const pageSize = ref(10)
// 总记录数
const total = ref(0)

// 每页显示多少条发生变化
const handleSizeChange = (val) => {
  pageSize.value = val
  logPage()
}

// 当前页码发生变化
const handleCurrentChange = (val) => {
  currentPage.value = val
  logPage()
}

// 获取日志分页数据
const logPage = async () => {
  try {
    const res = await logApi(currentPage.value, pageSize.value)
    console.log(res)
    
    if (res.code == 1) {
      logList.value = res.data.rows
      total.value = res.data.total
    }
  } catch (error) {
    console.error('获取日志数据失败:', error)
  }
}

// 格式化时间显示
const formatTime = (time) => {
  if (!time) return ''
  // 如果是时间戳，转换为日期格式
  if (typeof time === 'number') {
    return new Date(time).toLocaleString()
  }
  return time
}

// 钩子函数
onMounted(() => {
  logPage()
})
</script>

<template>
  <div class="log-container">
    <div class="header-section">
      <h2 class="page-title">日志管理</h2>
      <div class="header-info">
        <span class="total-count">共 {{ total }} 条记录</span>
      </div>
    </div>
    
    <el-card class="table-card">
      <el-table 
        :data="logList" 
        border 
        stripe 
        style="width: 100%"
        height="600"
        highlight-current-row>
        <el-table-column 
          type="index" 
          label="序号" 
          width="60" 
          align="center"
          fixed>
        </el-table-column>
        
        <el-table-column 
          label="操作人" 
          prop="operateEmpName" 
          min-width="100"
          show-overflow-tooltip>
          <template #default="scope">
            <el-tag size="small" type="primary">{{ scope.row.operateEmpName }}</el-tag>
          </template>
        </el-table-column>
        
        <el-table-column 
          label="操作时间" 
          prop="operateTime" 
          min-width="160"
          sortable>
          <template #default="scope">
            <i class="el-icon-time"></i>
            <span>{{ formatTime(scope.row.operateTime) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column 
          label="类名" 
          prop="className" 
          min-width="180"
          show-overflow-tooltip>
          <template #default="scope">
            <el-tag size="small" type="info">{{ scope.row.className }}</el-tag>
          </template>
        </el-table-column>
        
        <el-table-column 
          label="方法名" 
          prop="methodName" 
          min-width="120"
          show-overflow-tooltip>
        </el-table-column>
        
        <el-table-column 
          label="操作耗时" 
          prop="costTime" 
          width="120" 
          align="center"
          sortable>
          <template #default="scope">
            <el-tag 
              :type="scope.row.costTime > 1000 ? 'danger' : scope.row.costTime > 500 ? 'warning' : 'success'"
              size="small">
              {{ scope.row.costTime }} ms
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column 
          label="请求参数" 
          prop="methodParams" 
          min-width="250">
          <template #default="scope">
            <el-popover
              placement="top-start"
              title="请求参数详情"
              :width="300"
              trigger="hover">
              <template #reference>
                <div class="param-preview">{{ scope.row.methodParams }}</div>
              </template>
              <div class="param-detail">
                <pre>{{ scope.row.methodParams }}</pre>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        
        <el-table-column 
          label="返回值" 
          prop="returnValue" 
          min-width="250" 
          show-overflow-tooltip>
          <template #default="scope">
            <el-popover
              placement="top-start"
              title="返回值详情"
              :width="300"
              trigger="hover">
              <template #reference>
                <div class="param-preview">{{ scope.row.returnValue }}</div>
              </template>
              <div class="param-detail">
                <pre>{{ scope.row.returnValue }}</pre>
              </div>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    
    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        class="pagination">
      </el-pagination>
    </div>
  </div>
</template>

<style scoped>
.log-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100%;
}

.header-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 20px;
  background: linear-gradient(135deg, #0887e2 50%, #5eee05 100%);
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  color: white;
}

.page-title {
  color: white;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.header-info .total-count {
  font-size: 14px;
  opacity: 0.9;
}

.table-card {
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: none;
  margin-bottom: 20px;
  overflow: hidden;
}

.table-card :deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

.table-card :deep(.el-table__header th) {
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7f4 100%);
  color: #303133;
  font-weight: 600;
  font-size: 14px;
}

.table-card :deep(.el-table__row:hover) {
  background-color: #f8f9ff;
}

.table-card :deep(.el-table__row .cell) {
  padding: 8px 12px;
}

.param-preview {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
  color: #409eff;
}

.param-detail pre {
  margin: 0;
  padding: 10px;
  background-color: #f4f4f5;
  border-radius: 4px;
  max-height: 300px;
  overflow: auto;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.pagination-container {
  display: flex;
  justify-content: center;
  padding: 20px 0;
  background: white;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.pagination {
  padding: 0;
}

.pagination :deep(.el-pagination__total) {
  font-weight: 500;
}

.pagination :deep(.el-pagination__sizes) {
  margin-left: 15px;
}

@media (max-width: 768px) {
  .log-container {
    padding: 10px;
  }
  
  .header-section {
    flex-direction: column;
    align-items: flex-start;
    padding: 15px;
  }
  
  .header-info {
    margin-top: 10px;
  }
  
  .page-title {
    font-size: 20px;
  }
  
  .table-card :deep(.el-table) {
    font-size: 12px;
  }
  
  .table-card :deep(.el-table th),
  .table-card :deep(.el-table td) {
    padding: 6px 8px;
  }
  
  .param-preview {
    max-width: 100px;
  }
}
</style>